/**
 * ImageFilterModel.java
 * 
 * @date: 2012-02-08
 * @author: Xiaoyu Guo (MUN# 200982155)
 * This file is part of the course project for COMP 6752, 
 * Application of Computer Graphics. All the file is written
 * by myself.
 */
package mun.cs6752.project.ui;

import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

import mun.cs6752.project.filter.interfaces.IImageFilter;

/**
 * @author Xiaoyu Guo
 *
 */
public class ImageFilterModel 
    extends AbstractListModel 
    implements ComboBoxModel {

    private static final long serialVersionUID = -8145066731207722279L;
    
    private List<IImageFilter> filters;
    private IImageFilter selected;
    
    public ImageFilterModel(List<IImageFilter> filters){
        this.filters = filters;
        this.selected = null;
    }
    
    public IImageFilter getSelectedFilter(){
        return this.selected;
    }

    /* (non-Javadoc)
     * @see javax.swing.ListModel#getSize()
     */
    @Override
    public int getSize() {
        return filters.size();
    }

    /* (non-Javadoc)
     * @see javax.swing.ListModel#getElementAt(int)
     */
    @Override
    public Object getElementAt(int index) {
        return filters.get(index).getName();
    }

    /* (non-Javadoc)
     * @see javax.swing.ComboBoxModel#setSelectedItem(java.lang.Object)
     */
    @Override
    public void setSelectedItem(Object anItem) {
        if(anItem instanceof IImageFilter){
            this.selected = (IImageFilter) anItem;
        } else if (anItem instanceof String){
            for(IImageFilter f : filters){
                if(anItem.equals(f.getName())){
                    this.selected = f;
                    break;
                }
            }
        }else {
            return;
        }
    }

    /* (non-Javadoc)
     * @see javax.swing.ComboBoxModel#getSelectedItem()
     */
    @Override
    public Object getSelectedItem() {
        if(this.selected == null){
            return "";
        } else {
            return this.selected.getName();
        }
    }

}
